
package org.emet.flex.util
{
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;

	public class XlsUtils
	{
		public function XlsUtils()
		{
		}
		
		public static function getHeaders(dg:DataGrid, ignoreList:Array): Array {
			var dgCols:Array = dg.columns;
			var colDataValueArray:ArrayCollection =	new ArrayCollection();
			for (var headerIndex:int = 0; headerIndex < dgCols.length; headerIndex++) {
				var col:DataGridColumn = dgCols[headerIndex] as DataGridColumn;
				if(inIgnoreList(ignoreList, col.dataField)){
					continue;
				}
				colDataValueArray.addItem(col.headerText);  
			}
			
			return colDataValueArray.toArray();
		}
		
		public static function getDataArray(dataGrid:DataGrid, ignoreList:Array):Array
		{
			var dgCols:Array = dataGrid.columns;
			var dgDataList:ArrayCollection = dataGrid.dataProvider as ArrayCollection;
			
			var dataList:ArrayCollection = new ArrayCollection();
			for(var rowIndex:int= 0; rowIndex < dgDataList.length; rowIndex++){
				var row:Object = dgDataList.getItemAt(rowIndex);
				for (var colIndex:int =0; colIndex < dgCols.length; colIndex ++){
					var col:DataGridColumn = dgCols[colIndex] as DataGridColumn;
					if(inIgnoreList(ignoreList, col.dataField)){
						continue;
					}
					var val:Object = null;
					if(col.labelFunction != null){
						var labelFunc:Function = col.labelFunction;
						val = labelFunc(row, col);
					} else {
						var fieldName:String = col.dataField;
						var dotIndex:int = fieldName.indexOf(".");
						if(dotIndex != -1) {
							//TODO 支持无限
							var firstPro:String = fieldName.substring(0,dotIndex);
							var secondPro:String = fieldName.substring(dotIndex+1);
							var dot2Index:int = secondPro.indexOf(".");
							if( dot2Index != -1){
								var thirdPro:String = secondPro.substring(dot2Index+1);
								secondPro = fieldName.substring(dotIndex+1,dot2Index);
								val = row[firstPro][secondPro][thirdPro];
							} else {
								val = row[firstPro][secondPro];
							}
						} else {
							val = row[col.dataField];
						}
					}
					
					dataList.addItem(val);
				}
			}
			return dataList.toArray();
		}
		
		private static function inIgnoreList(ignoreList:Array, dataField:String):Boolean
		{
			for each(var obj:String in ignoreList) {
				if (obj == dataField) {
					return true;
				}
			}
			return false;
		}
		
//		public static function exportXls(dg:DataGrid, xlsFile:String):void {
//			var dgDataList:ArrayCollection = dg.dataProvider as ArrayCollection;
//			if(dgDataList == null || dgDataList.length ==0){return;}
//			
//			var dgCols:Array = dg.columns;
//			var excelFile:ExcelFile = new ExcelFile();
//			var sheet:Sheet  = new Sheet();  
//			sheet.resize(dgDataList.length, dgCols.length)
//			excelFile.sheets.addItem(sheet);
//			
//			//Create Header
//			var colDataValueArray:Array = [];
//			for (var headerIndex:int = 0; headerIndex < dgCols.length; headerIndex++) {
//				var col:DataGridColumn = dgCols[headerIndex] as DataGridColumn;
//				addCell(sheet, 0, headerIndex, col.headerText);  
//			}
//			
//			for(var rowIndex:int=1; rowIndex <= dgDataList.length; rowIndex++){
//				var row:Object = dgDataList.getItemAt(rowIndex - 1);
//				for (var colIndex:int =0; colIndex < dgCols.length; colIndex ++){
//					var col:DataGridColumn = dgCols[colIndex] as DataGridColumn;
//					var val:Object = null;
//					if(col.labelFunction != null){
//						var labelFunc:Function = col.labelFunction;
//						val = labelFunc(row, col);
//					} else {
//						val = row[col.dataField];
//					}
//					
//					addCell(sheet, rowIndex, colIndex, val);  
//				}
//				
//			}
//			
//			var mbytes:ByteArray  = excelFile.saveToByteArray();    
//			var file:FileReference = new FileReference();  
//			try {
//				file.save(mbytes, xlsFile + CommonUtils.formatDate(new Date(),"YYYYMMDDJJNNSS")+".xls");
//			} catch (error:Error) {
//				trace("Failed:", error.message)  
//			}
//		}
//		static function addCell(sheet:Sheet, i:int,j:int,o:Object):void{  
//			sheet.setCell(i, j, String(o));  
//		}
		
	}
}